﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Services.Description;
using FaceSharp.Api;
using FaceSharp.Api.Types;
using FaceSharp.MVC.Code.Repositories;
using Types = FaceSharp.Api.Types;
namespace FaceSharp.MVC.Code.Facebook
{
    public class FacebookCore : IFacebookCore
    {
        private readonly string _applicationId;
        private readonly string _applicationSecret;
        private readonly string _applicationUrl;
        private readonly string _extendedPermissions;

        public string ApplicationId { get { return _applicationId; } }
        public string ApplicationSecret { get { return _applicationSecret; } }
        public string ApplicationUrl { get { return _applicationUrl; } }
        public string ExtendedPermissions { get { return _extendedPermissions; } }

        public FacebookCore()
        {
            _applicationId = ConfigurationManager.AppSettings["ApplicationId"];
            _applicationSecret = ConfigurationManager.AppSettings["ApplicationSecret"];
            _applicationUrl = ConfigurationManager.AppSettings["ApplicationUrl"];
            _extendedPermissions = ConfigurationManager.AppSettings["ExtendedPermissions"];

            if (_applicationId != null && HttpContext.Current.Items["ApplicationId"] == null) HttpContext.Current.Items.Add("ApplicationId", _applicationId);
            if (_applicationSecret != null && HttpContext.Current.Items["ApplicationSecret"] == null) HttpContext.Current.Items.Add("ApplicationSecret", _applicationSecret);
            if (_applicationUrl != null && HttpContext.Current.Items["ApplicationUrl"] == null) HttpContext.Current.Items.Add("ApplicationUrl", _applicationUrl);
            if (_extendedPermissions != null && HttpContext.Current.Items["ExtendedPermissions"] == null) HttpContext.Current.Items.Add("ExtendedPermissions", _extendedPermissions);
        }

        public FaceSharp.Api.Types.User CurrentUser
        {
            get
            {
                var user = HttpContext.Current.Items["current_user"];
                if (user == null) return null;
                return (FaceSharp.Api.Types.User)user;
            }
            set
            {
                if (HttpContext.Current.Items["current_user"] == null)
                {
                    HttpContext.Current.Items.Add("current_user", value);
                }
                else
                {
                    HttpContext.Current.Items["current_user"] = value;
                }
            }
        }

        public string Code
        {
            get
            {
                var code = HttpContext.Current.Items["code"];
                return code == null ? null : code.ToString();
            }
            set
            {
                if (HttpContext.Current.Items["code"] == null)
                {
                    HttpContext.Current.Items.Add("code", value);
                }
                else
                {
                    HttpContext.Current.Items["code"] = value;
                }
            }
        }

        public string AccessToken
        {
            get
            {
                return HttpContext.Current.Items["access_token"] == null ? "" : HttpContext.Current.Items["access_token"].ToString();
            }
            set
            {
                if (HttpContext.Current.Items["access_token"] == null)
                {
                    HttpContext.Current.Items.Add("access_token", value);
                }
                else
                {
                    HttpContext.Current.Items["access_token"] = value;
                }
            }
        }

        public DateTime? AccessTokenExpirationDate
        {
            get
            {
                var accessTokenExpirationDate = HttpContext.Current.Items["access_token_expiration_date"];
                if (accessTokenExpirationDate == null) return null;
                return (DateTime?)accessTokenExpirationDate;
            }
            set
            {
                if (HttpContext.Current.Items["access_token_expiration_date"] == null)
                {
                    HttpContext.Current.Items.Add("access_token_expiration_date", value);
                }
                else
                {
                    HttpContext.Current.Items["access_token_expiration_date"] = value;
                }
            }
        }

        public SignedRequest SignedRequest
        {
            get
            {
                var signedRequest = HttpContext.Current.Items["signed_request"];
                if (signedRequest == null) return null;
                return (FaceSharp.Api.Types.SignedRequest)signedRequest;
            }
            set
            {
                if (HttpContext.Current.Items["signed_request"] == null)
                {
                    HttpContext.Current.Items.Add("signed_request", value);
                }
                else
                {
                    HttpContext.Current.Items["signed_request"] = value;
                }
            }
        }
    }
}